package com.meter.basic.controller;

import com.meter.basic.domain.BasicDevice;
import com.meter.basic.domain.BasicHouse;
import com.meter.basic.domain.BasicMeter;
import com.meter.basic.service.IBasicDeviceService;
import com.meter.basic.service.IBasicMeterService;
import com.meter.common.annotation.Log;
import com.meter.common.core.controller.BaseController;
import com.meter.common.core.domain.AjaxResult;
import com.meter.common.core.page.TableDataInfo;
import com.meter.common.enums.BusinessType;
import com.meter.common.utils.ShiroUtils;
import com.meter.common.utils.StringUtils;
import com.meter.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping({"/basic/meter"})
public class BasicMeterController extends BaseController {

    private String prefix = "basic/meter";

    @Autowired
    private IBasicMeterService basicMeterService;

    @Autowired
    private IBasicDeviceService basicDeviceService;

    /**
     * 热表信息
     * @return
     */
    @RequiresPermissions({"basic:meter:view"})
    @GetMapping(value = { "", "/{deviceId}" })
    public String meter(@PathVariable(value = "deviceId", required = false) Long deviceId, ModelMap mmap)
    {
        if(StringUtils.isNotNull(deviceId)) {
            BasicDevice basicDevice = basicDeviceService.getById(deviceId);
            mmap.put("basicDevice", basicDevice);
        }
        return prefix + "/meter";
    }

    /**
     * 热表信息列表
     * @param basicMeter
     * @return
     */
    @RequiresPermissions({"basic:meter:list"})
    @PostMapping({"/list"})
    @ResponseBody
    public TableDataInfo list(BasicMeter basicMeter) {
        startPage();
        List<BasicMeter> list = basicMeterService.selectBasicMeterList(basicMeter);
        return getDataTable(list);
    }

    /**
     * @Author 王琦
     * @Description //热表信息导入
     * @Date 2020/6/19 19:56
     * @Param [file, updateSupport]
     * @return com.platform.common.core.domain.AjaxResult
     */
    @Log(title = "热表信息导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("basic:meter:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasicMeter> util = new ExcelUtil<BasicMeter>(BasicMeter.class);
        List<BasicMeter> meterList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = basicMeterService.importBaseMeter(meterList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * @Author 王琦
     * @Description //导入模板
     * @Date 2020/6/19 19:56
     * @Param []
     * @return com.platform.common.core.domain.AjaxResult
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BasicMeter> util = new ExcelUtil<BasicMeter>(BasicMeter.class);
        return util.importTemplateExcel("热表信息");
    }

    /**
     * 新增热表信息
     * @param mmap
     * @return
     */
    @GetMapping({"/add"})
    public String add(ModelMap mmap) {
        List<BasicDevice> deviceList = basicDeviceService.selectBasicDeviceList(new BasicDevice());
        mmap.put("deviceList", deviceList);
        return prefix + "/add";
    }

    /**
     * 新增热表信息
     * @param basicMeter
     * @return
     */
    @RequiresPermissions({"basic:meter:add"})
    @Log(title = "热表信息", businessType = BusinessType.INSERT)
    @PostMapping({"/add"})
    @ResponseBody
    public AjaxResult addSave(BasicMeter basicMeter) {
        basicMeter.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(basicMeterService.insertBasicMeter(basicMeter));
    }

    /**
     * 修改热表信息
     * @param meterId
     * @param mmap
     * @return
     */
    @GetMapping({"/edit/{meterId}"})
    public String edit(@PathVariable("meterId") Long meterId, ModelMap mmap) {
        List<BasicDevice> deviceList = basicDeviceService.selectBasicDeviceList(new BasicDevice());
        mmap.put("deviceList", deviceList);
        BasicMeter basicMeter = basicMeterService.selectBasicMeterById(meterId);
        mmap.put("basicMeter", basicMeter);
        return prefix + "/edit";
    }

    /**
     * 修改热表信息
     * @param basicMeter
     * @return
     */
    @RequiresPermissions({"basic:meter:edit"})
    @Log(title = "热表信息", businessType = BusinessType.UPDATE)
    @PostMapping({"/edit"})
    @ResponseBody
    public AjaxResult editSave(BasicMeter basicMeter) {
        basicMeter.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(basicMeterService.updateBasicMeter(basicMeter));
    }

    /**
     * 检测热表是否唯一
     * @param basicMeter
     * @return
     */
    @PostMapping({"/checkMeterNoUnique"})
    @ResponseBody
    public boolean checkMeterNoUnique(BasicMeter basicMeter) {
        return basicMeterService.checkMeterNoUnique(basicMeter);
    }

    /**
     * 删除热表信息
     * @param ids
     * @return
     */
    @RequiresPermissions({"basic:meter:remove"})
    @Log(title = "热表信息", businessType = BusinessType.DELETE)
    @PostMapping({"/remove"})
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(basicMeterService.deleteBasicMeterByIds(ids));
    }
}
